import Vue from 'vue'
import config from './config';

export default function({app,$axios,redirect,store}) {
  const utils = {
    toInt: function(val) {
      val = parseInt(val);
      if (isNaN(val)) val = 0;
      return val;
    },
    toFloat: function(val) {
      val = parseFloat(val);
      if (isNaN(val)) val = 0;
      return Math.round(val * 100) / 100;
    },
    rmoney: function(s) {
      s = s || 0;
      var val = typeof s !== 'number' ? parseFloat(s.replace(/[^\d\.-]/g, "")) : s;
      if (isNaN(val)) val = 0;
      return Math.round(val * 100) / 100;
    },
    formatSize: function(size) {
      size = utils.toInt(size);
      if (size < 1024) return size + "B";
      size = size / 1024;
      if (size < 1024) return utils.toFloat(size) + "KB";
      size = size / 1024;
      if (size < 1024) return utils.toFloat(size) + "MB";
    },
    getFileExt: function(name) {
      name = name || '';
      var i = name.lastIndexOf('\.'),
        ext = "";
      if (i !== -1) {
        ext = name.substring(i + 1).toLocaleLowerCase();
      }
      return ext;
    },
    checkAdminPermission: function(val) {
      if (store.state.userdetail.isadmin) return true;//判断是否超管

      try {
        if (parseFloat(val).toString() == "NaN") {
          var arr = val.split('.'); //val为检查的权限
          val = store.state.userdetail.sys; //权限列表
          arr.forEach((item) => {
            val = val[item]; //循环获取权限value
          });
          if (typeof val === 'object') {
            val = val.value;
          }
        }
        //判断是否有权限
        // var operation = ','+store.state.userdetail.operation
        // return operation.indexOf(','+val) !== -1;
        var operation = store.state.userdetail.operation.split(',')
        var array =  operation.filter((res) => {
          return res == val
        })
       return array.length>0;
      } catch (e) {
        //console.log('权限设置错误：' + $(this).attr('permission'));
        return false;
      }
    },
    curDateTime(dateVal,format) {
      format = format || '';
      var str = '',
        date = dateVal,
        year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),
        hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
        minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
        second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
      month >= 1 && month <= 9 ? (month = '0' + month) : '';
      day >= 0 && day <= 9 ? (day = '0' + day) : '';

      switch(format) {
        case 'yyyy':
          str = year;
          break;
        case 'yyyy-MM':
          str = year + '-' + month;
          break;
        case 'yyyy-MM-dd':
          str = year + '-' + month + '-' + day;
          break;
        case 'yyyy-MM-dd HH:mm':
          str = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
          break;
        case 'MM-dd':
          str = month + '-' + day;
          break;
        default:
          str = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
          break;
      }
      return str;
    },
    saveAndExport(content, filename) {
      //接口返回的数据格式是文件流，转成通过标签直接下载
      const blob = new Blob([content])
      const _fileName = filename
      if ('download' in document.createElement('a')) {
        // 非IE下载
       const elink = document.createElement('a')
        elink.download = _fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
       elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
      } else {
        // IE10+下载
        navigator.msSaveBlob(blob, _fileName)
       }
    },
    /**
     * 金额按千位逗号分割,数字千分位格式化
     * @public
     * @param mixed mVal 数值
     * @return string
     *
     * Example
     * <code>
     * alert(utils.formatMoney(1234.345)); //=>1,234.35
     * alert(utils.formatMoney(-1234.345)); //=>-1,234.35
     * </code>
     */
    formatMoney: function(mVal, suf) {
      var num = mVal?.toString();
      if (!num) return "0.00";
      num = num.toString().replace(/\$|\,/g, ''); // 去除$符号
      if (isNaN(num)) num = "0.00";
      var sign = (num == (num = Math.abs(num)));
      num = Math.floor(num * 100 + 0.50000000001);
      var cents = num % 100;
      num = Math.floor(num / 100).toString();
      if (cents < 10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) num = num.substring(0, num.length - (
        4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
      return ((suf ? suf : "") + ((sign) ? '' : '-') + '' + num + '.' + cents);
    },
  }

  Vue.prototype.utils = utils
  Vue.prototype.websetting = {
    address: "",
    adminlogoimg: "/images/logo.png",
    copyright: "",
    name: "",
    sitelogoimg: "/images/logo-ico.png",
    telphone: "",
    wxqrcode: "",
  }

  Vue.prototype.baseURL = config.baseURL
  Vue.prototype.fileURL = config.fileURL
  Vue.prototype.fileUploadURL = config.fileUploadURL
}
